﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace iMusik.Mp3ZingOnline
{
    public class Mp3Song
    {
        private string _SongName;  
        private string _SingerName;
        private string _DirectLink;
        private string _LocalLink;
        public string SongName
        {
            get { return _SongName; }
            private set { _SongName = value; }
        }
        public string SingerName
        {
            get { return _SingerName; }
            private set { _SingerName = value; }
        }
        public string DirectLink
        {
            get { return _DirectLink; }
            private set { _DirectLink = value; }
        }

        public Mp3Song(string localLink)
        {
            _SongName = "";
            _SingerName = "";
            _DirectLink = "";
            _LocalLink = localLink;
        }
        public Mp3Song(string songName, string singerName, string myLink,bool IsLocalLink)
        {
            _SongName = songName;
            _SingerName = singerName;
            if (IsLocalLink)
            {
                _LocalLink = myLink;
                _DirectLink = "";
            }
            else
            {
                _LocalLink = "";
                _DirectLink = myLink;
            }
                
        
        }
        public async Task SetAllInformation()
        {
            try
            {
                string source = await Mp3Zing.DownloadString(new Uri(_LocalLink));
                int i = 0;
                while (i < source.Length)
                {
                    if (source[i] == 34)
                    {
                        source = source.Remove(i, 1);
                        i--;
                    }
                    i++;
                }

                if (_SongName == "")
                    SetSongName(source);
                if (_SingerName == "")
                    SetSingerName(source);
                if (_DirectLink == "")
                    await SetDirectLink(source);
            }
            catch (Exception ex)
            {
                throw (ex);
            }

        }

        private void SetSongName(string source)
        {
            string pattern = @"(?<=<h1>).*?(?=</h1>)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);
            _SongName = mc[0].Value;

        }

        private void SetSingerName(string source)
        {
            string pattern = @"(?<=<h2>).*?(?=</h2>)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);
            _SingerName = mc[0].Value;

        }

        private async Task SetDirectLink(string source)
        {
            string pattern = @"(?<=xml=).*?(?=class)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);
            string tempSource = mc[0].Value;

            int i = 0;
            while (i < tempSource.Length)
            {
                if (tempSource[i] == 34 || tempSource[i] == 32)
                {
                    tempSource = tempSource.Remove(i, 1);
                    i--;
                }
                i++;
            }
            tempSource = await Mp3Zing.DownloadString(new Uri(tempSource));
            char t =(char)34;
            pattern = "(?=http).*?(?==)";
            Regex regex1 = new Regex(pattern);
            MatchCollection mc1 = regex1.Matches(tempSource);

            string tempLink = mc1[0].Value;

            i = 0;
            while (i < tempLink.Length)
            {
                if (tempLink[i] == 92 || tempLink[i] == 61)
                {
                    tempLink = tempLink.Remove(i, 1);
                    i--;
                }
                i++;
            }
            _DirectLink = tempLink;

        }
        
    }
}
